package com.crv.ci.mq.producer.impl;

import com.crv.ci.mq.IpQueueDestination;
import com.crv.ci.mq.producer.IProducerService;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;


import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;


@SuppressWarnings("unchecked")
@Service("ProducerServiceImpl")
public class ProducerServiceImpl implements IProducerService {
	
//	private static final Logger logger = LoggerFactory.getLogger(ProducerServiceImpl.class);
	
	@Autowired
	private JmsTemplate jmsTemplate;

	@Override
	public void sendMessage(Destination destination, final String message) {

//		logger.info("send {} .....",message);
		jmsTemplate.send(destination, new MessageCreator() {
			@Override
			public Message createMessage(Session session) throws JMSException {
				return session.createTextMessage(message);
			}
		});
	}

	@Override
	public void sendMessage(String ip, String message) {
//		logger.info("send {} .....",message);
		Destination destination = new IpQueueDestination(ip);
		this.sendMessage(destination, message);
	}


	public JmsTemplate getJmsTemplate() {
		return jmsTemplate;
	}

	public void setJmsTemplate(JmsTemplate jmsTemplate) {
		this.jmsTemplate = jmsTemplate;
	}
}
